package com.wing.user.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.user.model.entity.Client;
import com.wing.user.service.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端-Controller
 *
 * @author
 */
@Api(value = "客户端管理", tags = "#/user/client")
@RestController("clientController")
@RequestMapping("/user/admin/client")
@RequiredArgsConstructor
public class ClientController {

    private final ClientService clientService;

    @ApiOperation(value = "查询所有客户端", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<Client> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,@RequestParam(value = "clientId", required = false) String clientId
    ){

        Page<Client> pg = new Page<>(page, limit);
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        //搜索
        if (StringUtils.isNotBlank(clientId)) {
            queryWrapper.like("client_id", clientId);
        }

        IPage<Client> list = clientService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询客户端", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", required = true, dataType = "String", paramType = "form")
    })
    @GetMapping("/getByClientId")
    public JsonResult<Client> getByClientId(String clientId) {
        Client client = clientService.getOne(new QueryWrapper<Client>().eq("client_id", clientId));
        return JsonResult.success(client);
    }

    @ApiOperation(value = "添加客户端", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "client", value = "客户端", required = true, dataType = "Client", paramType = "form"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("/add")
    public JsonResult add(Client client) {
        if (clientService.save(client)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改客户端", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "client", value = "客户端", required = true, dataType = "Client", paramType = "form"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PutMapping("/update")
    public JsonResult update(Client client) {
        if (clientService.updateById(client)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "删除客户端", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端id", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long clientId) {
        if (clientService.removeById(clientId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
